/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aohanyao.viewpager;

import com.aohanyao.utils.PxUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示卡片的容器
 */
public class HorizontalViewPager extends ComponentContainer implements ComponentContainer.TouchEventListener {
    int onLocation = 0;
    static final int mMaxVisible = 4;
    private List<Component> mChildComponents = new ArrayList<Component>();
    private List<Component> mBaseComponents = new ArrayList<Component>();
    private List<Component> mRemoveChildComponents = new ArrayList<Component>();
    private List<Component> mTempChildComponents = new ArrayList<Component>();
    int size = 15;

    public HorizontalViewPager(Context context) {
        super(context);
        initView();
    }

    public HorizontalViewPager(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView();
    }

    /**
     * 添加卡片
     * @param data 添加的卡片集合
     */
    public void setData(List<Component> data){
        this.mChildComponents = data;
        this.mBaseComponents = data;
        initView();
    }

    /**
     * 获取添加的卡片
     * @return 获取当前显示卡片的集合
     */
    public List<Component> getRestoreAdd(){
        return mChildComponents;
    }

    /**
     * 获取移除的卡片
     * @return 返回移除的卡片集合
     */
    public List<Component> getResotreRemove(){
        return mRemoveChildComponents;
    }

    /**
     * 保存当前的卡片信息
     * @param add 需要显示的卡片集合
     * @param remove 移除卡片的集合
     */
    public void setRestoreStata(List<Component> add,List<Component> remove){
        this.mChildComponents = add;
        this.mRemoveChildComponents = remove;
        removeAllComponents();
        initView();
    }

    /**
     * 设置卡片显示方式
     * @param type 设置卡片展示的方向
     */
    public void setType(int type) {
        this.onLocation = type;
//        resetData();
        initView();
    }

    private void resetData(){
        for(int j =0;j< mChildComponents.size();j++) {
            mTempChildComponents.add(mChildComponents.get(j));
        }
        mChildComponents.clear();
        if(mRemoveChildComponents.size() >= 1) {
            for (int i = 0; i <mRemoveChildComponents.size(); i++) {
                mChildComponents.add(mRemoveChildComponents.get(i));
            }
        }
        for(int m =0;m< mTempChildComponents.size();m++){
            mChildComponents.add(mTempChildComponents.get(m));
        }
//        mChildComponents = mBaseComponents;
//        mChildComponents.replaceAll(mBaseComponents);
        mTempChildComponents.clear();
        mRemoveChildComponents.clear();

    }

    /**
     * 初始化显示内容
     */
    private void initView() {
        PxUtil.initContext(getContext());
        setTouchEventListener(this::onTouchEvent);
        if(mChildComponents.size() < 1) {
            return;
        }
        addFourComponent();
    }

    int defaultWidth = 600;
    int width = 0;
    int height = 1250;
    int heightMargin = 80;
    int widthMargin = 60;
    int leftMargin = 10;
    int panddingLeft = 0;

    /**
     * 设置显示的四张卡片的位置
     */
    //需要实现 显示的宽度更改，需要向下位移
    private void addFourComponent(){
        defaultWidth = (int) (PxUtil.screenWidth() - PxUtil.vp2px(120));//juzhong
        height = (int) (PxUtil.screenHeight() - PxUtil.vp2px(180));
//        removeAllComponents();
        switch (onLocation) {
            case 0://Top
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    //600 - 10 * m, 600 - 10 * m
                    //居中展示
                    width = defaultWidth - 40 * m;
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    mChildComponents.get(m).setTranslationX((PxUtil.screenWidth() - width)/2);
                    mChildComponents.get(m).setTranslationY(heightMargin*(mMaxVisible-1) - heightMargin * m);
//                    modifyX = (PxUtil.screenWidth() - width)/2;
//                    modifyY = heightMargin*(mMaxVisible-1) - heightMargin * m;
                    LayoutConfig layoutConfig = new LayoutConfig(width, height);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);

                }
                break;
            case 1://Top Left
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    width = defaultWidth - 40 * m;
                    //(PxUtil.screenWidth() - width)/2-widthMargin * m
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    mChildComponents.get(m).setTranslation((int)PxUtil.vp2px(60 - 20*m)-leftMargin,heightMargin*(mMaxVisible-1)-heightMargin * m);//居中展示
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);

                }
                break;
            case 2://Top Right
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    width = defaultWidth - 40 * m;
                    mChildComponents.get(m).setTranslation((PxUtil.screenWidth() -defaultWidth)/2 +PxUtil.vp2px(35*m), heightMargin*(mMaxVisible-1)-heightMargin * m);
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height);
                    layoutConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT, DependentLayout.LayoutConfig.TRUE);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);
                }
                break;
            case 3://Bottom
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    //600 - 10 * m, 600 - 10 * m
                    width = defaultWidth - 40 * m;
                    //screenHe -PxUtil.screenHeight() + PxUtil.vp2px(180)
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                        mChildComponents.get(m).setTranslation((PxUtil.screenWidth() - width)/2,(PxUtil.screenHeight()- height) - 3*heightMargin +heightMargin * m);
                    }
                    mChildComponents.get(m).setTranslation((PxUtil.screenWidth() - width)/2,(PxUtil.screenHeight()- height) - 3*heightMargin +heightMargin * m);//居中展示
                    panddingLeft = (PxUtil.screenWidth() - width)/2;
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height);
                    layoutConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT, DependentLayout.LayoutConfig.TRUE);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);
                }
                break;
            case 4://Bottom Left
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    width = defaultWidth - 40 * m;
                    mChildComponents.get(m).setTranslation((int)PxUtil.vp2px(60 - 20*m)-leftMargin, (PxUtil.screenHeight()- height) - 3*heightMargin+heightMargin * m);
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height);
                    layoutConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT, DependentLayout.LayoutConfig.TRUE);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);
                }
                break;
            case 5://Bottom Right
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    width = defaultWidth - 40 * m;
                    mChildComponents.get(m).setTranslation((PxUtil.screenWidth() -defaultWidth)/2 +PxUtil.vp2px(35*m), (PxUtil.screenHeight()- height) - 3*heightMargin+heightMargin * m); //右边展示
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height);
                    layoutConfig.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT, DependentLayout.LayoutConfig.TRUE);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);
                }
                break;
            case 6://Right
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    width = defaultWidth - 40 * m;
                    mChildComponents.get(m).setTranslation((PxUtil.screenWidth() -defaultWidth)/2 +PxUtil.vp2px(35*m),(PxUtil.screenHeight() - (height))/2 + 20*m); //右边展示
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height-40 * m);
                    layoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_START, DependentLayout.LayoutConfig.TRUE);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);
                }
                break;
            case 7://Left
                for (int m = mChildComponents.size() - 1; m >= 0; m--) {
                    if(mChildComponents.get(m).getRotation() != 0) {
                        mChildComponents.get(m).setRotation(0);
                        mChildComponents.get(m).setAlpha(1);
                    }
                    width = defaultWidth - 40 * m;
                    mChildComponents.get(m).setTranslation((int)PxUtil.vp2px(60- 20*m)-leftMargin,(PxUtil.screenHeight() - (height))/2  + 20*m); //右边展示
                    DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(width, height-40 * m);
                    layoutConfig.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_END, DependentLayout.LayoutConfig.TRUE);
                    removeComponent(mChildComponents.get(m));
                    addComponent(mChildComponents.get(m),layoutConfig);
                }
                break;
        }
    }

//    @Override
//    public boolean onArrange(int i, int i1, int i2, int i3) {//通知容器组件在布局时设置子组件的位置和大小,适合指定位置的时候使用
//        if(onLocation == 0) {
//            for(int j=0;j<mChildComponents.size();j++) {
//                mChildComponents.get(j).arrange(100, 500 - 100*j, 700, 1200+100*j);
//            }
//        }
//        return false;
//    }

    /**
     * 设置是否可以触摸
     * @param enable 是否可以触摸
     */
    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    float lastx = 0;
    boolean enable = true;
    int move = 0;  //1  正向移动  2 负向移动

    /**
     * 触摸事件的处理
     * @param component 触摸的Component
     * @param touchEvent 触摸事件
     * @return 触摸的返回值
     */
    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if(!enable) {
            return false;
        }
        float x = touchEvent.getPointerScreenPosition(0).getX();
        boolean ret = false;
        float movingX = 0;
        boolean cancle = false;

        switch (touchEvent.getAction()){
            case TouchEvent.PRIMARY_POINT_DOWN:
//                LogUtil.loge("gongmengfei"+"PRIMARY_POINT_DOWN"+x +" "+lastx+" "+(x-lastx));
                lastx = x;
                ret = true;
                break;
            case TouchEvent.POINT_MOVE:
                if (x - lastx < -100 && mChildComponents.size() > 1 && (move != 1)) {  //左侧移动的时候移除
//                    LogUtil.loge("gongmengfei"+"POINT_MOVE ="+(x - lastx) +" "+move);
                    move = 2;
                    removeTouch(x - lastx);
                    movingX = x;
                }
                if ((move != 2) && (x - lastx) > 100 && mChildComponents.size() < size) {
                    move = 1;
//                    LogUtil.loge("gongmengfei"+"POINT_MOVE"+(x - lastx) +" "+move);
                    addTouch(x - lastx);
                    movingX = x;
                }
                ret = true;
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                //需要标记一个起始位置
//                LogUtil.loge("gongmengfei"+"POINT_UP"+x +" "+lastx+" "+(x-lastx));
                if (x - lastx <= -300 && mChildComponents.size() > 1 && move == 2) {  //左侧移动的时候移除
                    if (x - lastx <= -300) {
                        mRemoveChildComponents.add(mChildComponents.get(0));
                        if (mChildComponents.size() > 1) {
                            removeComponent(mChildComponents.get(0));
                        }
                        mChildComponents.remove(0);
                    }
//                    LogUtil.loge("gongmengfei"+"POINT_UP remove"+x +" "+lastx+" "+(x-lastx));
//                LogUtil.loge("gongmengfei" + "onUpdate" + mChildComponents.size());
                    addFourComponent();
//                    removeAnimation(0,0);
                } else if ((x - lastx) > 100 && mChildComponents.size() < size && move == 1) {
                    if ((x - lastx) < 300) {
                        removeComponent(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                        return false;
                    }
                    for (int j = 0; j < mChildComponents.size(); j++) {
                        mTempChildComponents.add(mChildComponents.get(j));
                    }
                    mChildComponents.clear();
                    mChildComponents.add(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                    for (int m = 0; m < mTempChildComponents.size(); m++) {
                        mChildComponents.add(mTempChildComponents.get(m));
                    }
                    mTempChildComponents.clear();

                    mRemoveChildComponents.remove(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
//                    LogUtil.loge("gongmengfei"+"POINT_UP add"+x +" "+lastx+" "+(x-lastx));
                    addFourComponent();
                } else {
//                    LogUtil.loge("gongmengfei"+"POINT_UP else"+x +" "+lastx+" "+(x-lastx));
                    if (move == 1) {
                        removeComponent(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                    }
                    mChildComponents.get(0).setAlpha(1);
                    mChildComponents.get(0).setRotation(0);
                    addFourComponent();
                }
                move = 0;
                lastx = 0;
                ret = false;
                break;
            case TouchEvent.CANCEL:
//                LogUtil.loge("gongmengfei"+"CANCLE"+x +" "+lastx+" "+(x-lastx));
                mChildComponents.get(0).setAlpha(1);
                mChildComponents.get(0).setRotation(0);
                addFourComponent();
                ret = true;
                break;
        }
        return ret;
    }

    /**
     * 移除卡片的处理
     * @param movex 左移动的距离
     */
    private void  removeTouch(float movex) {
        if (movex < 0 && move != 1) {
            if (mChildComponents.size() > 1) {
                float progress = (Math.abs(movex / 400) > 1) ? 1 : Math.abs(movex / 400);
                mChildComponents.get(0).setTranslationX((int) (movex * 0.6));//, -v * 600);
                mChildComponents.get(0).setTranslationY(mChildComponents.get(0).getTranslationY());
                mChildComponents.get(0).setRotation(-45 * Math.abs(movex / 1200));
                mChildComponents.get(0).setAlpha(1 - Math.abs(movex / 1200));
                for (int i = 1; i < mChildComponents.size(); i++) {
                    if (i < 5) {
                        if (onLocation == 0) {
                            mChildComponents.get(i).setTranslationX(mChildComponents.get(i).getTranslationX());//X轴不动
                            mChildComponents.get(i).setTranslationY(heightMargin * (4 - i) * progress); //y轴下移对应的高度间距
                        } else if (onLocation == 1) {
                            if(i == 0) {
                                continue;
                            }
                            if(i >0 && i < 4) {
                                mChildComponents.get(i).setTranslationX(widthMargin * (4 - i) * progress);//widthMargin * (4-i) * progress);
                                mChildComponents.get(i).setTranslationY(40 * (4 - i) * progress);
                            }
                            if(i == 4) {
                                mChildComponents.get(i).setTranslationX(-widthMargin+widthMargin*progress);//widthMargin * (4-i) * progress);
                                mChildComponents.get(i).setTranslationY(-heightMargin+heightMargin*progress);
                            }
                        } else if (onLocation == 2) {
                            if(i != 5) {
                                mChildComponents.get(i).setTranslation((PxUtil.screenWidth() - defaultWidth) / 2 + PxUtil.vp2px(35 * i) - widthMargin * (4 - i) * progress, heightMargin * (mMaxVisible - 1) - heightMargin * i + 40 * (4 - i) * progress);
                            }
                            if(i == 4) {
                                mChildComponents.get(i).setTranslationX((PxUtil.screenWidth() - defaultWidth) / 2 + PxUtil.vp2px(35 * i) -widthMargin*progress);//widthMargin * (4-i) * progress);
                                mChildComponents.get(i).setTranslationY(-heightMargin+heightMargin*progress);
                            }
                        } else if (onLocation == 3) {
                            mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(), (PxUtil.screenHeight() - height) + heightMargin * (i - 4) * progress);
                        } else if (onLocation == 4) {
                            if(i == 0) {
                                continue;
                            }
                            mChildComponents.get(i).setTranslation((int) PxUtil.vp2px(80 - 20 * i) * progress - leftMargin, (PxUtil.screenHeight() - height) + heightMargin * (i - 4) * progress);
                        } else if (onLocation == 5) {
                            mChildComponents.get(i).setTranslation((PxUtil.screenWidth() - defaultWidth) / 2 + PxUtil.vp2px(35 * i) - widthMargin * progress, (PxUtil.screenHeight() - height) + heightMargin * (i - 4) * progress);
                        } else if (onLocation == 6) {
                            mChildComponents.get(i).setTranslation((PxUtil.screenWidth() - defaultWidth) / 2 + PxUtil.vp2px(35 * i) - widthMargin * progress, mChildComponents.get(i).getTranslationY());
                        } else if (onLocation == 7) {
                            mChildComponents.get(i).setTranslation((int) PxUtil.vp2px(80 - 20 * i) * progress - leftMargin, (PxUtil.screenHeight() - (height)) / 2 + 20 * i * progress);
                        }
                    }
                }
            }
        }
    }

    /**
     * 添加卡片的处理
     * @param movex 右移动的距离
     */
    private void addTouch(float movex) {
        if (move == 2) {
            return;
        }
        if (mRemoveChildComponents.size() >= 1) {
            if (movex > 0) {
                float progress = (Math.abs(movex / 400) > 1) ? 1 : Math.abs(movex / 400);
                removeComponent(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                addComponent(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).getComponentParent().moveChildToFront(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setAlpha(progress);
                mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationX(-600 + (600 + panddingLeft) * progress);//, -600 + v * 600);
                if (onLocation == 1) {
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY(3 * heightMargin * progress);
                } else if (onLocation == 2) {
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY(3 * heightMargin * progress);
                } else if (onLocation == 3) {
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY((PxUtil.screenHeight() - height) - 2 * heightMargin - heightMargin * progress);
                } else if (onLocation == 4) {
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY((PxUtil.screenHeight() - height) - 2 * heightMargin - heightMargin * progress);
                } else if (onLocation == 5) {
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY((PxUtil.screenHeight() - height) - 2 * heightMargin - heightMargin * progress);
                }
                mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setRotation(-45 + 45 * progress);
                for (int i = 0; i < mChildComponents.size(); i++) {
                    if (i < 4) {
                        if (onLocation == 0) {
                            if (i < 3) {
                                mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(), 2 * heightMargin - heightMargin * i * progress);
                            } else if (i == 3) {
                                mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(), -mChildComponents.get(i).getTranslationY() - heightMargin * progress);
                            }
                        } else if (onLocation == 1) {
                            mChildComponents.get(i).setTranslation(2 * widthMargin - widthMargin * i * progress, 2 * heightMargin - heightMargin * i * progress);
                        } else if (onLocation == 2) {
                            mChildComponents.get(i).setTranslation(defaultWidth / 2 - 2 * widthMargin + 100 * i * progress, 2 * heightMargin - heightMargin * i * progress);
                        } else if (onLocation == 3) {
                            mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(), (PxUtil.screenHeight() - height) - 2 * heightMargin + heightMargin * i * progress);
                        } else if (onLocation == 4) {
                            mChildComponents.get(i).setTranslation(2 * widthMargin - widthMargin * i * progress, (PxUtil.screenHeight() - height) - 2 * heightMargin + heightMargin * i * progress);
                        } else if (onLocation == 5) {
                            mChildComponents.get(i).setTranslation(defaultWidth / 2 - 2 * widthMargin + 100 * i * progress, (PxUtil.screenHeight() - height) - 2 * heightMargin + heightMargin * i * progress);
                        } else if (onLocation == 6) {
                            mChildComponents.get(i).setTranslation(defaultWidth / 2 - 2 * widthMargin + 100 * i * progress, mChildComponents.get(i).getTranslationY());
                        } else if (onLocation == 7) {
                            mChildComponents.get(i).setTranslation(2 * widthMargin - widthMargin * i * progress, mChildComponents.get(i).getTranslationY());
                        }
                    }
                }
            }
        }
    }


    private void startAnimation(float x) {
        if (x < 0 && move != 1) {
            if (mChildComponents.size() > 1) {
                float progress = (Math.abs(x / 400) > 1) ? 1 : Math.abs(x / 400);
                mChildComponents.get(0).setTranslationX((int) (x * 0.6));//, -v * 600);
                mChildComponents.get(0).setTranslationY(mChildComponents.get(0).getTranslationY());
                mChildComponents.get(0).setRotation(-45 * Math.abs(x / 1200));
                mChildComponents.get(0).setAlpha(1 - Math.abs(x / 1200));
                for (int i = 0; i < mChildComponents.size(); i++) {
                    if (i < 4) {
                        if(onLocation == 0) {
                            mChildComponents.get(i).setTranslationX(mChildComponents.get(i).getTranslationX());//X轴不动
                            mChildComponents.get(i).setTranslationY(heightMargin*(4-i)*progress); //y轴下移对应的高度间距
                        }else if(onLocation == 1) {
                            mChildComponents.get(i).setTranslationX(widthMargin*(4-i)*progress);
                            mChildComponents.get(i).setTranslationY(40*(4-i)*progress);
                        }else if(onLocation == 2) {
                            mChildComponents.get(i).setTranslation((PxUtil.screenWidth() -defaultWidth)/2 +PxUtil.vp2px(35*i)-widthMargin*(4-i)*progress, heightMargin*(mMaxVisible-1)-heightMargin * i+40*(4-i)*progress);
                        }else if(onLocation == 3) {
                            mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(),(PxUtil.screenHeight()- height) +heightMargin * (i-4)*progress);
                        }else if(onLocation == 4) {
                            mChildComponents.get(i).setTranslation((int)PxUtil.vp2px(80 - 20*i)*progress-leftMargin, (PxUtil.screenHeight()- height) +heightMargin * (i-4)*progress);
                        }else if(onLocation == 5) {
                            mChildComponents.get(i).setTranslation((PxUtil.screenWidth() -defaultWidth)/2 +PxUtil.vp2px(35*i)-widthMargin*progress, (PxUtil.screenHeight()- height)+heightMargin * (i-4)*progress);
                        }else if(onLocation == 6) {
                            mChildComponents.get(i).setTranslation((PxUtil.screenWidth() -defaultWidth)/2 +PxUtil.vp2px(35*i)-widthMargin*progress,mChildComponents.get(i).getTranslationY());
                        }else if(onLocation == 7) {
                            mChildComponents.get(i).setTranslation((int)PxUtil.vp2px(80- 20*i)*progress-leftMargin,(PxUtil.screenHeight() - (height))/2  + 20*i*progress);
                        }
                    }
                }
            }

        } else if (x > 0 && move != 2) {
            if (mRemoveChildComponents.size() >= 1) {
                if (x > 0) {
                    float progress = (Math.abs(x / 400) > 1) ? 1 : Math.abs(x / 400);
                    removeComponent(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                    addComponent(mRemoveChildComponents.get(mRemoveChildComponents.size() - 1));
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setAlpha(progress);
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationX(-600 + (600 + panddingLeft) * progress);//, -600 + v * 600);
                    if(onLocation == 1) {
                        mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY(3*heightMargin * progress);
                    }else if (onLocation == 2) {
                        mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY(3*heightMargin * progress);
                    }else if (onLocation == 3) {
                        mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY((PxUtil.screenHeight()- height)-2*heightMargin- heightMargin*progress);
                    }else if(onLocation == 4) {
                        mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY((PxUtil.screenHeight()- height)-2*heightMargin- heightMargin*progress);
                    }else if(onLocation == 5) {
                        mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setTranslationY((PxUtil.screenHeight()- height)-2*heightMargin- heightMargin*progress);
                    }
                    mRemoveChildComponents.get(mRemoveChildComponents.size() - 1).setRotation(-45 + 45 * progress);
                    for (int i = 0; i < mChildComponents.size(); i++) {
                        if (i < 4) {
                            if(onLocation ==0) {
                                if(i <3) {
                                    mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(), 2 * heightMargin - heightMargin * i * progress);
                                }else if(i ==3) {
                                    mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(),  -mChildComponents.get(i).getTranslationY()- heightMargin * progress);
                                }
                            }else if (onLocation == 1) {
                                mChildComponents.get(i).setTranslation(2 * widthMargin - widthMargin * i * progress, 2*heightMargin-heightMargin * i * progress);
                            }else if(onLocation == 2) {
                                mChildComponents.get(i).setTranslation( defaultWidth/2 - 2*widthMargin+100 * i * progress, 2*heightMargin-heightMargin*i*progress);
                            }else if(onLocation == 3) {
                                mChildComponents.get(i).setTranslation(mChildComponents.get(i).getTranslationX(), (PxUtil.screenHeight()- height) - 2*heightMargin+heightMargin*i*progress);
                            }else if(onLocation == 4) {
                                mChildComponents.get(i).setTranslation( 2 * widthMargin - widthMargin * i * progress, (PxUtil.screenHeight()- height) - 2*heightMargin+heightMargin*i*progress);
                            }else if (onLocation == 5) {
                                mChildComponents.get(i).setTranslation( defaultWidth/2 - 2*widthMargin+100 * i * progress, (PxUtil.screenHeight()- height) - 2*heightMargin+heightMargin*i*progress);
                            }else if(onLocation == 6) {
                                mChildComponents.get(i).setTranslation( defaultWidth/2 - 2*widthMargin+100 * i * progress, mChildComponents.get(i).getTranslationY());
                            }else if(onLocation == 7) {
                                mChildComponents.get(i).setTranslation( 2 * widthMargin - widthMargin * i * progress, mChildComponents.get(i).getTranslationY());
                            }
                        }
                    }
                }
            }
        }
    }
}
